﻿/**
 * jQuery EasyUI 1.2.4
 *
 * Licensed under the GPL terms
 * To use it on other terms please contact us
 *
 * Copyright(c) 2009-2011 stworthy [ stworthy@gmail.com ]
 *
 */
(function($) {
	//resize
    function _1(_2, _3) {
        var _4 = $.data(_2, "triggerfield").options;
        var _5 = $.data(_2, "triggerfield").triggerfield;
        if (_3) {
            _4.width = _3;
        }
        _5.appendTo("body");
        if (isNaN(_4.width)) {
            _4.width = _5.find("input.combo-text").outerWidth();
        }
        var _7 = 0;
        if (_4.hasDownArrow) {
            _7 = _5.find(".combo-arrow").outerWidth();
        }
        var _3 = _4.width - _7;
        if ($.boxModel == true) {
            _3 -= _5.outerWidth() - _5.width();
        }
        _5.find("input.combo-text").width(_3);
        _5.insertAfter(_2);
    };
	//hasDownArrow
    function _8(_9) {
        var _a = $.data(_9, "triggerfield").options;
        var _b = $.data(_9, "triggerfield").triggerfield;
        if (_a.hasDownArrow) {
            _b.find(".combo-arrow").show();
        } else {
            _b.find(".combo-arrow").hide();
        }
    };
	//append
    function _c(_d) {
        $(_d).addClass("triggerfield-f").hide();
        var _e = $("<span class=\"combo\"></span>").insertAfter(_d);
        var _f = $("<input type=\"text\" class=\"combo-text\">").appendTo(_e);
        $("<span><span class=\"combo-arrow\"></span></span>").appendTo(_e);
        var _g = $("<input type=\"hidden\" class=\"combo-value\">").appendTo(_e);
        var _11 = $(_d).attr("name");
        if (_11) {
            _e.find("input.combo-value").attr("name", _11);
            $(_d).removeAttr("name").attr("triggerfieldName", _11);
        }
        _f.attr("autocomplete", "off");
		if ($(_d).attr("value").indexOf('||') != -1) {
			_f.val($(_d).attr("value").split('||')[0]);
			_g.val($(_d).attr("value").split('||')[1]);
		} else {
			_f.val($(_d).attr("text"));
			_g.val($(_d).attr("value"));
		}
        return {
            triggerfield: _e
        };
    };
	//destroy
    function _12(_13) {
        var _14 = $.data(_13, "triggerfield").triggerfield.find("input.combo-text");
        _14.validatebox("destroy");
        $.data(_13, "triggerfield").triggerfield.remove();
        $(_13).remove();
    };
	//disable,enable
    function _15(_16) {
        var _17 = $.data(_16, "triggerfield").options;
        var _18 = $.data(_16, "triggerfield").triggerfield;
        var _1a = _18.find(".combo-text");
        var _1b = _18.find(".combo-arrow");
        _18.unbind(".triggerfield");
        _1a.unbind(".triggerfield");
        _1b.unbind(".triggerfield");
        if (!_17.disabled) {
            _1a.bind("mousedown.triggerfield", function(e) {
                e.stopPropagation();
            }).bind("click.triggerfield", function() {
                _1b.click();
            });
            _1b.bind("click.triggerfield", function() {
                _1a.focus();
                _1c(_16);
            }).bind("mouseenter.triggerfield", function() {
                $(this).addClass("combo-arrow-hover");
            }).bind("mouseleave.triggerfield", function() {
                $(this).removeClass("combo-arrow-hover");
            });
        }
    };
	//click
    function _1c(_1d) {
        var _1e = $.data(_1d, "triggerfield").options;
        var _1f = $.data(_1d, "triggerfield").triggerfield;
        _1e.onClick.call(_1d);
        function _21() {
            var top = _1f.offset().top + _1f.outerHeight();
            if (top + _20.outerHeight() > $(window).height() + $(document).scrollTop()) {
                top = _1f.offset().top - _20.outerHeight();
            }
            if (top < $(document).scrollTop()) {
                top = _1f.offset().top + _1f.outerHeight();
            }
            return top;
        };
    };
	//validate
    function _26(_27, _28) {
        var _29 = $.data(_27, "triggerfield").options;
        var _2a = $.data(_27, "triggerfield").triggerfield.find("input.combo-text");
        _2a.validatebox(_29);
        if (_28) {
            _2a.validatebox("validate");
            _2a.trigger("mouseleave");
        }
    };
	//disable,enable
    function _2b(_2c, _2d) {
        var _2e = $.data(_2c, "triggerfield").options;
        var _2f = $.data(_2c, "triggerfield").triggerfield;
        if (_2d) {
            _2e.disabled = true;
            $(_2c).attr("disabled", true);
            _2f.find(".combo-value").attr("disabled", true);
            _2f.find(".combo-text").attr("disabled", true);
        } else {
            _2e.disabled = false;
            $(_2c).removeAttr("disabled");
            _2f.find(".combo-value").removeAttr("disabled");
            _2f.find(".combo-text").removeAttr("disabled");
        }
    };
	//clear
    function _30(_31) {
        var _32 = $.data(_31, "triggerfield").options;
        var _33 = $.data(_31, "triggerfield").triggerfield;
        _33.find("input.combo-value").val("");
        _33.find("input.combo-text").val("");
    };
	//getText
    function _34(_35) {
        var _36 = $.data(_35, "triggerfield").triggerfield;
        return _36.find("input.combo-text").val();
    };
	//setText
    function _37(_38, _39) {
        var _3a = $.data(_38, "triggerfield").triggerfield;
        _3a.find("input.combo-text").val(_39);
        _26(_38, true);
        $.data(_38, "triggerfield").previousValue = _39;
    };
    function _3b(_3c) {
        var _3d = [];
        var _3e = $.data(_3c, "triggerfield").triggerfield;
        _3e.find("input.combo-value").each(function() {
            _3d.push($(this).val());
        });
        return _3d;
    };
    function _3f(_40, _41) {
        var _42 = $.data(_40, "triggerfield").options;
        var _43 = _3b(_40);
        var _44 = $.data(_40, "triggerfield").triggerfield;
        _44.find("input.combo-value").remove();
        var _45 = $(_40).attr("triggerfieldName");
        for (var i = 0; i < _41.length; i++) {
            var _46 = $("<input type=\"hidden\" class=\"combo-value\">").appendTo(_44);
            if (_45) {
                _46.attr("name", _45);
            }
            _46.val(_41[i]);
        }
        var tmp = [];
        for (var i = 0; i < _43.length; i++) {
            tmp[i] = _43[i];
        }
        var aa = [];
        for (var i = 0; i < _41.length; i++) {
            for (var j = 0; j < tmp.length; j++) {
                if (_41[i] == tmp[j]) {
                    aa.push(_41[i]);
                    tmp.splice(j, 1);
                    break;
                }
            }
        }
    };
	//getValue
    function _47(_48) {
        var _49 = _3b(_48);
        return _49[0];
    };
	//setValue
    function _4a(_4b, _4c) {
        _3f(_4b, [_4c]);
    };
    $.fn.triggerfield = function(_50, _51) {
        if (typeof _50 == "string") {
            return $.fn.triggerfield.methods[_50](this, _51);
        }
        _50 = _50 || {};
        return this.each(function() {
            var _52 = $.data(this, "triggerfield");
            if (_52) {
                $.extend(_52.options, _50);
            } else {
                var r = _c(this);
                _52 = $.data(this, "triggerfield", {
                    options: $.extend({}, $.fn.triggerfield.defaults, $.fn.triggerfield.parseOptions(this), _50),
                    triggerfield: r.triggerfield,
                    previousValue: null
                });
                $(this).removeAttr("disabled");
            }
            $("input.combo-text", _52.triggerfield).attr("readonly", !_52.options.editable);
            _8(this);
            _2b(this, _52.options.disabled);
            _1(this);
            _15(this);
            _26(this);
        });
    };
    $.fn.triggerfield.methods = {
        options: function(jq) {
            return $.data(jq[0], "triggerfield").options;
        },
        textbox: function(jq) {
            return $.data(jq[0], "triggerfield").triggerfield.find("input.combo-text");
        },
        destroy: function(jq) {
            return jq.each(function() {
                _12(this);
            });
        },
        resize: function(jq, _53) {
            return jq.each(function() {
                _1(this, _53);
            });
        },
        click: function(jq) {
            return jq.each(function() {
                _1c(this);
            });
        },
        disable: function(jq) {
            return jq.each(function() {
                _2b(this, true);
                _15(this);
            });
        },
        enable: function(jq) {
            return jq.each(function() {
                _2b(this, false);
                _15(this);
            });
        },
        validate: function(jq) {
            return jq.each(function() {
                _26(this, true);
            });
        },
        isValid: function(jq) {
            var _54 = $.data(jq[0], "triggerfield").triggerfield.find("input.combo-text");
            return _54.validatebox("isValid");
        },
        clear: function(jq) {
            return jq.each(function() {
                _30(this);
            });
        },
        getText: function(jq) {
            return _34(jq[0]);
        },
        setText: function(jq, _55) {
            return jq.each(function() {
                _37(this, _55);
            });
        },
        getValue: function(jq) {
            return _47(jq[0]);
        },
        setValue: function(jq, _57) {
            return jq.each(function() {
                _4a(this, _57);
            });
        }
    };
    $.fn.triggerfield.parseOptions = function(_58) {
        var t = $(_58);
        return $.extend({}, $.fn.validatebox.parseOptions(_58), {
            width: (parseInt(_58.style.width) || undefined),
            editable: (t.attr("editable") ? t.attr("editable") == "true" : undefined),
            disabled: (t.attr("disabled") ? true : undefined),
            hasDownArrow: (t.attr("hasDownArrow") ? t.attr("hasDownArrow") == "true" : undefined),
			text: (t.attr("text") || undefined),
            value: (t.val() || undefined)
        });
    };
    $.fn.triggerfield.defaults = $.extend({}, $.fn.validatebox.defaults, {
        width: "auto",
        editable: false,
        disabled: false,
        hasDownArrow: true,
		text: "",
        value: "",
        onClick: function() {
        }
    });
})(jQuery);

